package com.longbuluo;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import com.longbuluo.util.JdbcUtil;

public class DataAccess {
	/**
	 * 批量新增
	 * @param movies
	 * @throws SQLException
	 */
	public int[] insert(List<Movie> movies) throws SQLException{
		int len = movies.size();
		Object[][] params = new Object[len][6];
		for(int i=0; i<len; i++){
			Movie movie = movies.get(i);
			params[i][0] = movie.getId();
			params[i][1] = movie.getTitle();
			params[i][2] = movie.getCreateDate();
			params[i][3] = movie.getPic();
			params[i][4] = movie.getLinks();
			params[i][5] = movie.getCategory();
		}

		String sql = "INSERT INTO t_movie(id, title, createDate, pic, links, category) VALUES(?,?,?,?,?,?)";
		QueryRunner query = new QueryRunner(true);
		return query.batch(JdbcUtil.getConnection(), sql, params);
	}
	
	/**
	 * 清空
	 * @param category
	 * @throws SQLException
	 */
	public void clear(String category) throws SQLException{
		String sql = "DELETE FROM t_movie where category=?";
		QueryRunner query = new QueryRunner(true);
		query.update(JdbcUtil.getConnection(), sql, category);
	}
}
